import os, sys
import cPickle as pickle
import subprocess 

from anycloud import config
from anycloud.engine.base import Engine
from anycloud.error import NotFoundError

## TODO: refactor to use the new multiprocess module
class MultiProcessEngine(Engine):
    _params = (
        config.IntParameter(
            'multiprocess.poolsize', 'Number of processes to use.',
        ), 
        config.StringParameter(
            'multiprocess.python', 'Path to python binary',
            default='python'
        ), 
    )

    def retrieve(self, tasks):
        tasks = [t for t in tasks] # new list, so we can pop safely
        numtasks = len(tasks)    
        poolsize = self.poolsize or numtasks
        running = {}
        done = []
             
        while len(done) < numtasks:
            # submit tasks (if below poolsize and tasks remain)
            for i in xrange(min(poolsize-len(running), len(tasks))):
                task = tasks.pop()
                wd = task._export()
                cwd = os.getcwd()
                os.chdir(wd)
                pid = subprocess.Popen([self.python, '__task__.py']).pid
                running[pid] = task
                os.chdir(cwd)
            
            # wait for any child process to finish
            pid,status = os.wait() 
            done.append(running.pop(pid, None))

        for task in done:
            task._process_result(task.wd)
            task._cleanup()
        
        return [t.result for t in done]
